﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Windows.Forms.DataVisualization.Charting;

namespace ControlPress
{
    public partial class MainForm : Form
    {

        List<myPoint> mPointsOne;
        List<myLine> mLines;
        List<myPoint> mNewPoints;


        List<myPoint> mPointsTwo;
        List<myPoint> mPointsThree;

        List<myPoint> mPoints_1_2;
        List<myPoint> mPoints_1_3;
        List<myPoint> mPoints_2_3;
        List<myPoint> mPoints_1_2_3;

        List<myPoint> mPoints_Sample;

        List<myPoint> mDesignSample;


        // 第一个曲线图的 60mpa的情况
        List<myPoint> mSample1_60;
        List<myPoint> mSample1_140;
        List<myPoint> mSample1_200;
        


        const double MAX_OFFSET = 0.8;

        public MainForm()
        {
            InitializeComponent();


            // 对Chart进行设置
            //myChart.Series["处理前压力"].ChartType = SeriesChartType.Line;
            //myChart.Series["处理前压力"].BorderWidth = 1;
            //myChart.Series["处理前压力"].Color = Color.Blue;

            //// 对Chart进行设置
            //myChart.Series["处理后压力"].ChartType = SeriesChartType.Line;
            //myChart.Series["处理后压力"].BorderWidth = 1;
            //myChart.Series["处理后压力"].Color = Color.Red;

            //myChart.Series["#1"].ChartType = SeriesChartType.Line;
            //myChart.Series["#1"].BorderWidth = 2;
            //myChart.Series["#1"].Color = Color.Red;

            //myChart.Series["2#"].ChartType = SeriesChartType.Line;
            //myChart.Series["2#"].BorderWidth = 2;
            //myChart.Series["2#"].Color = Color.Blue;

            myChart.Series["#3"].ChartType = SeriesChartType.Line;
            myChart.Series["#3"].BorderWidth = 2;
            myChart.Series["#3"].Color = Color.Coral;

            myChart.Series["#12"].ChartType = SeriesChartType.Line;
            myChart.Series["#12"].BorderWidth = 2;
            myChart.Series["#12"].Color = Color.DarkGreen;

            //myChart.Series["#12"].ChartType = SeriesChartType.Line;
            //myChart.Series["#12"].BorderWidth = 2;
            //myChart.Series["#12"].Color = Color.DarkGreen;

            //myChart.Series["13#"].ChartType = SeriesChartType.Line;
            //myChart.Series["13#"].BorderWidth = 2;
            //myChart.Series["13#"].Color = Color.Navy;

            //myChart.Series["23#"].ChartType = SeriesChartType.Line;
            //myChart.Series["23#"].BorderWidth = 2;
            //myChart.Series["23#"].Color = Color.SkyBlue;

            //myChart.Series["123#"].ChartType = SeriesChartType.Line;
            //myChart.Series["123#"].BorderWidth = 2;
            //myChart.Series["123#"].Color = Color.Turquoise;

            //myChart.Series["Sample"].ChartType = SeriesChartType.Line;
            //myChart.Series["Sample"].BorderWidth = 2;
            //myChart.Series["Sample"].Color = Color.Violet;

            myChart.Series["Sample"].ChartType = SeriesChartType.FastLine;
            myChart.Series["Sample"].BorderWidth = 1;
            myChart.Series["Sample"].Color = Color.Violet;

            // 初始化List Lines
            mLines = new List<myLine>();
            mNewPoints = new List<myPoint>();

            mPoints_1_2 = new List<myPoint>();
            mPoints_1_3 = new List<myPoint>();
            mPoints_2_3 = new List<myPoint>();
            mPoints_1_2_3 = new List<myPoint>();


            mDesignSample = new List<myPoint>();

            mSample1_60 = new List<myPoint>();
            mSample1_140 = new List<myPoint>();
            mSample1_200 = new List<myPoint>();
        }

        //private void btnChange_Click(object sender, EventArgs e)
        //{
        //    // 读取txt数据 放入List中
        //    string strFile = "1#.txt";
        //    mPointsOne = LoadFile(strFile);

        //    mPointsTwo = LoadFile("2#.txt");
        //    mPointsThree = LoadFile("3#.txt");

        //    //mPoints_Sample = LoadFile("Sample.txt");

        //    // 显示数据 刻度需要调整
  

        //    // 对数据进行直线化处理
        //    //CrateLines();


        //    // 创建新的点集合.
        //    //CreateNewPoints();

        //    // 对比显示
        //    //ShowLine();

        //    // 保存txt文件.
        //    //string strFileName = "Handled.txt";
        //    //SaveFile(mNewPoints, strFileName);

        //    // 整合阀门1_2

        //    //mPoints_1_2 = GetDate(mPointsOne, mPointsTwo);
        //    //SaveFile(mPoints_1_2, "12#.txt");

        //    //mPoints_1_3 = GetDate(mPointsOne, mPointsThree);
        //    //SaveFile(mPoints_1_3, "13#.txt");

        //    //mPoints_2_3 = GetDate(mPointsTwo, mPointsThree);
        //    //SaveFile(mPoints_2_3, "23#.txt");



        //    //mPoints_1_2_3 = GetDate(mPoints_1_2, mPointsThree);
        //    //SaveFile(mPoints_1_2_3, "123#.txt");


        //    //Console.WriteLine("Here");

        //    //ShowLine();


        //    // Create Gongyi curve.
        //    // mPoints_Sample = CreateRandom(mPointsThree, mPoints_1_2_3);
        //    // SaveFile(mPoints_Sample, "Sample.txt");

            


        //    // 加入4条不同曲线图 DesignSample.

        //    // 200 - 150  #123
        //    //double dCurrentValue = 200.0;
        //    //int nCurrentIndex = 0;

        //    //while ((dCurrentValue <= 200.0) && (dCurrentValue > 150.0))
        //    //{
        //    //    // 去 #123里找寻200左右最近的值 返回Index
        //    //    nCurrentIndex = FindIndexFromLine(dCurrentValue, mPoints_1_2_3);



        //    //}

        //    //// # 123. 返回大于150的一个数字，有可能是151.1
        //    //double dLastValue = CopyValue(mDesignSample, mPoints_1_2_3, 200.01, 150.0);


        //    //// # 23. 100.4
        //    //dLastValue = CopyValue1(mDesignSample, mPoints_2_3, dLastValue, 100.0);
            

        //    //// # 13. 50.3
        //    //dLastValue = CopyValue1(mDesignSample, mPoints_1_3, dLastValue, 50.0);

        //    //// # 1.
        //    //dLastValue = CopyValue1(mDesignSample, mPointsOne, dLastValue, 0.0);


        //    // 对曲线图数据进行 +-2 pa的处理

        //    // 检验查看生成的DesignSample曲线


        //    ShowLine();


        //    // 已经有工艺曲线图 mDesignSample.
        //    // 根据这个工艺曲线和7种控制曲线=》 一套控制方法

        //    //List<ControlObject> listControl = new List<ControlObject>();

        //    GetControlMethod();
        //}

        /// <summary>
        /// 根据工艺曲线，获得控制流程
        /// </summary>
        //private void GetControlMethod()
        //{
        //    // 
        //    int nCount = mDesignSample.Count;

        //    double dCurrent = 0.0;
        //    double dNext = 0.0;

        //    for (int i = 0; i < (nCount-1); i++)
        //    {
        //        // mDesignSample[i] // 这个值是可以访问的.
        //        // mDesignSample[i+1] // 这个值也是可以访问的.

        //        dCurrent = mDesignSample[i].Y;
        //        dNext = mDesignSample[i+1].Y;

        //        ControlObject eachControl = new ControlObject();

        //        // 利用某一秒的泄压能力.
        //        String strControlMethod = FindControlMethod(dCurrent, dNext); 
        //    }

        //}

        //private string FindControlMethod(double dCurrent, double dNext)
        //{
        //    //String strRtn = string.Empty;

        //    // 如果当前值和下一个值 相同，直接返回最小的阀门.

        //    // 否则对7个阀门泄压能力做个评估，返回泄压力最接近那个.

        //    List<PressAbility> pressList = new List<PressAbility>();

        //    double dBest = Math.Abs(dCurrent - dNext);

        //    if (0.0 == dBest)
        //    {
        //        // 返回泄压能力最小的阀门.
        //        return "#1";
        //    }
            
        //    // #1
        //    double press1 = GetPressChaZhi(dCurrent, mPointsOne);
        //    PressAbility each1 = new PressAbility();
        //    each1.dChaZhi = press1;
        //    each1.strFaMen = "#1";
        //    each1.Index = 0;
        //    pressList.Add(each1);


        //    // #2
        //    double press2 = GetPressChaZhi(dCurrent, mPointsTwo);

        //    PressAbility each2 = new PressAbility();
        //    each2.dChaZhi = press2;
        //    each2.strFaMen = "#2";
        //    each2.Index = 1;
        //    pressList.Add(each2);

        //    // #3
        //    double press3 = GetPressChaZhi(dCurrent, mPointsThree);

        //    PressAbility each3 = new PressAbility();
        //    each3.dChaZhi = press3;
        //    each3.strFaMen = "#3";
        //    each3.Index = 2;
        //    pressList.Add(each3);

        //    // #12
        //    double press12 = GetPressChaZhi(dCurrent, mPoints_1_2);

        //    PressAbility each12 = new PressAbility();
        //    each12.dChaZhi = press12;
        //    each12.strFaMen = "#12";
        //    each12.Index = 3;
        //    pressList.Add(each12);

        //    // #13
        //    double press13 = GetPressChaZhi(dCurrent, mPoints_1_3);

        //    PressAbility each13 = new PressAbility();
        //    each13.dChaZhi = press13;
        //    each13.strFaMen = "#13";
        //    each13.Index = 4;
        //    pressList.Add(each13);

        //    // #23
        //    double press23 = GetPressChaZhi(dCurrent, mPoints_2_3);

        //    PressAbility each23 = new PressAbility();
        //    each23.dChaZhi = press23;
        //    each23.strFaMen = "#23";
        //    each23.Index = 5;
        //    pressList.Add(each23);

        //    // #123
        //    double press123 = GetPressChaZhi(dCurrent, mPoints_1_2_3);

        //    PressAbility each123 = new PressAbility();
        //    each123.dChaZhi = press123;
        //    each123.strFaMen = "#123";
        //    each123.Index = 6;
        //    pressList.Add(each123);



        //    // 继续找七个press的差值和dBest最接近的值
        //    Console.WriteLine();
        //    int nCount = pressList.Count;

        //    for (int i = 0; i < nCount; i++)
        //    {
        //        double dEach = pressList[i].dChaZhi;

        //        double dChaZhi = Math.Abs(dEach - dBest);

        //        // 数据不变 换个用法.
        //        pressList[i].dChaZhi = dChaZhi;
        //        pressList[i].Index = i;

        //    }


        //    double dMin = pressList[0].dChaZhi;
        //    for (int i = 0; i < nCount; i++)
        //    {
        //        double dEach = pressList[i].dChaZhi;

        //        if (dEach < dMin)
        //        {
        //            dMin = dEach;
        //        }
        //    }

        //    // 最后可以找到一个最小值，但是我不知道最小值是哪一个Index.
        //    // 用最小值再去查找是哪一个元素 并且返回阀门操作.

        //    String strRtn = string.Empty;
        //    for (int i = 0; i < nCount; i++)
        //    {
        //        double dEach = pressList[i].dChaZhi;

        //        if (dEach == dMin)
        //        {
        //            strRtn = pressList[i].strFaMen;
        //        }
        //    }


        //    return strRtn;
        //}


        ///// <summary>
        ///// 返回一个差值.
        ///// </summary>
        ///// <param name="dCurrent">当前压力值.</param>
        ///// <param name="mPointsOne">阀门一泄压能力模型</param>
        ///// <returns>返回一个差值.</returns>
        //private double GetPressChaZhi(double dCurrent, List<myPoint> mPointsOne)
        //{
        //    // 找到离dCurrent最接近的值. 并且下一个值为不同的值.

        //    // 如果找不到返回-1 代表没有完全匹配的值.

        //    List<PressAbility> templist = new List<PressAbility>();

        //    int nCount = mPointsOne.Count;
        //    for (int i = 0; i < nCount; i++)
        //    {
        //        double dEachChazhi = Math.Abs(mPointsOne[i].Y - dCurrent);

        //        PressAbility each = new PressAbility();
        //        each.Index = i;
        //        each.dChaZhi = dEachChazhi;

        //        templist.Add(each);

        //    }

        //    //
        //    Console.WriteLine();


        //    // 求出这个temp里chazhi最小的值的位置
        //    // 如果有多个最小值，找到最后一个.

        //    // 然后从这个位置提取出最接近dCurrent的值。
        //    // 减去下一个元素，
        //    // 把差值返回.
        //    nCount = templist.Count;
        //    double dMin = 0.0;
        //    int nBestIndex = 0;

        //    for (int i = 0; i < (nCount-1); i++)
        //    {
        //        //
        //        double dEach = templist[i].dChaZhi;
        //        // 如果先找到一个差值最小值了
        //        if (dEach <= dMin)
        //        {
        //            // 马上判断下一个值 是否比这个差值小或者相等， 如果是 则continue.
        //            double dNext = templist[i+1].dChaZhi;

        //            if (dNext <= dEach)
        //            {
        //                continue;
        //            }
        //            else
        //            {
        //                // 如果大于, 那么认为找到的Index为最合适的Index.

        //                nBestIndex = i;
        //                break;
        //            }

                   
        //        }
        //    }


        //    double dRtn = Math.Abs(mPointsOne[nBestIndex].Y - mPointsOne[nBestIndex + 1].Y);

        //    return dRtn;
        //}


        ///// <summary>
        ///// 创造模拟工艺曲线
        ///// </summary>
        ///// <param name="mDestination"></param>
        ///// <param name="mSource"></param>
        ///// <param name="dLastValue"></param>
        ///// <param name="p"></param>
        ///// <returns></returns>
        //private double CopyValue1(List<myPoint> mDestination, List<myPoint> mSource, double dLastValue, double p)
        //{
        //    // 150.56 ~ 100.0  在这之间找出一个值，这个值正好小于150.56一点点的值.

        //    int nCount = mSource.Count;
        //    double dRtn = 0.0;
        //    for (int i = 0; i < nCount; i++)
        //    {
        //        myPoint each = mSource[i];

        //        if (each.Y <= dLastValue)
        //        {
        //            dRtn = each.Y;
        //            break;
        //        }
        //    }

        //    dRtn = CopyValue(mDestination, mSource, dRtn, p);

        //    return dRtn;
        //}

        //private double CopyValue(List<myPoint> mDestination, List<myPoint> mSource, double dmax, double dmin)
        //{

        //    int nIndex = 0;
        //    double dCurrent = mSource[nIndex].Y;

        //    int nCount = mSource.Count;

        //    // 返回index数字.
        //    for (int i = 0; i < nCount; i++)
        //    {
        //        myPoint each = mSource[i];

        //        if (each.Y <= dmin)
        //        {
        //            dCurrent = mSource[i - 1].Y;
        //            break;
        //        }

        //        // 只有当值处于最大值和最小值之间时，才加进去.
        //        if ( (each.Y <= dmax) && (each.Y >= dmin) )
        //        {
        //            mDestination.Add(each);
        //        }
            
        //    }

        //    // 返回范围值之内的值.
        //    return dCurrent;
        //}

        //private int FindIndexFromLine(double dCurrentValue, List<myPoint> mmyPoints)
        //{



        //    // 找到一个差值最小的值.
        //    int nCount = mmyPoints.Count;
        //    if (0 == nCount)
        //    {
        //        return -1;
        //    }

        //    double dMin = mmyPoints[0].Y;

        //    // 遍历所有元素
        //    for (int i = 0; i < nCount; i++)
        //    {
        //        double dTemp = dCurrentValue - mmyPoints[i].Y;

        //        if (dMin < 0.0)
        //        {
        //            continue;
        //        }
        //        else if()
        //        {
                    
        //        }
        //    }


        //}

        //private List<myPoint> CreateRandom(List<myPoint> mPointsThree, 
        //                                    List<myPoint> mPoints_1_2_3)
        //{
        //    List<myPoint> reList = new List<myPoint>();
        //    int nCount = mPointsThree.Count;

        //    for (int i = 0; i < nCount; i++)
        //    {
        //        myPoint each = new myPoint();
        //        if (0 == i)
        //        {
        //            // 初始化为最开始的值.
        //            each.Y = mPointsThree[i].Y;
        //            each.X = mPointsThree[i].X;
        //            each.Index = i;
        //        }
        //        else
        //        {

        //            // 提取3的差值.
        //            double d_3 = Math.Abs(mPointsThree[i].Y - mPointsThree[i - 1].Y);

        //            // 提取123的差值
        //            double d_1_2_3 = Math.Abs(mPoints_1_2_3[i].Y - mPoints_1_2_3[i - 1].Y);

        //            // 两者中取个随机值.

        //            // 0~1
        //            Random rd = new Random();
        //            double dValue = rd.NextDouble();
        //            double dY = dValue * Math.Abs(d_1_2_3 - d_3);

        //            // 构造曲线，如果值为小于0时，break掉
        //            each.Index = i;
        //            each.X = mPointsThree[i].X;
        //            each.Y = reList[i - 1].Y - dY;

        //            if (each.Y < 0.0)
        //            {
        //                break;
        //            }
        //        } // else


        //        reList.Add(each);
        //    }

        //    return reList;
        //}

        //private List<myPoint> GetDate(
        //                                List<myPoint> mPointsOne, 
        //                                List<myPoint> mPointsTwo
        //                                )
        //{
        //    List<myPoint> reList = new List<myPoint>();
        //    int nCount = mPointsOne.Count;

        //    for (int i = 0; i < nCount; i++)
        //    {
        //        myPoint each = new myPoint();

        //        if (0 == i)
        //        {
        //            // 初始化为最开始的值.
        //            each.Y = mPointsOne[i].Y;
        //            each.X = mPointsOne[i].X;
        //            each.Index = i;
        //        }
        //        else
        //        {
        //            // 当为1时， 去查找 0->1的差值.
        //            each.Index = i;
        //            each.X = mPointsOne[i].X;

        //            // 得到#1的差值.
        //            double dOneOff = Math.Abs(mPointsOne[i].Y - mPointsOne[i - 1].Y);

        //            // 得到#2的差值.
        //            double dTwoOff = Math.Abs(mPointsTwo[i].Y - mPointsTwo[i - 1].Y);

        //            // 差值和
        //            double dSumOff = dOneOff + dTwoOff;
        //            each.Y = reList[i - 1].Y - dSumOff;
        //        }

        //        reList.Add(each);
        //    }

        //    return reList;
        //}

        private void SaveFile(List<myPoint> mNewPoints, string strFileName)
        {
            FileStream fs = new FileStream(strFileName, FileMode.Create);
            StreamWriter sw = new StreamWriter(fs);

            //开始写入
            
            foreach (myPoint itemPoint in mNewPoints)
            {
                sw.WriteLine(itemPoint.Y.ToString());
            }

            //清空缓冲区
            sw.Flush();
            //关闭流
            sw.Close();
            fs.Close();
        }

        private void CreateNewPoints()
        {
            foreach (myLine itemLine in mLines)
            {
                foreach (myPoint itemPoint in itemLine.NewPoints)
                {
                    // 用Index来唯一标示，不用重复加入.
                    bool bCheck = bIsin(itemPoint.Index, mNewPoints);
                    if (false == bCheck)
                    {
                        mNewPoints.Add(itemPoint);
                    }
                }
            }
        }

        /// <summary>
        /// 判断点是否已经加入到集合中
        /// </summary>
        /// <param name="p"></param>
        /// <param name="thisPoints"></param>
        /// <returns></returns>
        private bool bIsin(int p, List<myPoint> thisPoints)
        {
            bool bRtn = false;

            foreach (myPoint item in thisPoints)
            {
                if (p == item.Index)
                {
                    bRtn = true;
                    break;
                }
            }

            return bRtn;
        }

        //private void CrateLines()
        //{
        //    // 指向当前处理的直线的开始点
        //    int nCurrentPoint = 0;

        //    // 处理的点的个数设置
        //    int nHandledPoint = 0;

        //    // 需要处理的总的点的个数
        //    int nSum = mPointsOne.Count;

        //    // 获得已被处理的点的个数.

        //    // 一直循环 当把所有点都处理完成之后 结束条件
        //    // 当nHandlePoint等于nSum时，表明所有点全部处理完毕
        //    while (nHandledPoint < nSum)
        //    {
        //        // GetLine 返回生成直线的结束点的Index
        //        int nEndPointIndex = GetLine(nCurrentPoint, mPointsOne);

        //        // 设置下一个开始点的位置
        //        // 为生成直线后的结束点
        //        nCurrentPoint = nEndPointIndex;

        //        nHandledPoint = GetHandleNum(mPointsOne);
        //    }

        //    // Handle 检测.
        //    //foreach (myLine each in mLines)
        //    //{
        //    //    foreach (myPoint item in each.OriPoints)
        //    //    {
        //    //        if (false == item.Handle)
        //    //        {
        //    //            Console.WriteLine("Here to test");
        //    //        }
        //    //    }
        //    //}

        //    //int nmySum = 0;
        //    //foreach (myLine each in mLines)
        //    //{
        //    //    nmySum = nmySum + each.OriPoints.Count;
        //    //}

        //    //nmySum = nmySum - (mLines.Count - 1);

        //    // Create NewPoints.
        //    CreateNewPoints(mLines);


        //    //Console.WriteLine("Here to test");
        //}

        //private void CreateNewPoints(List<myLine> mLines)
        //{
        //    foreach (myLine itemLine in mLines)
        //    {
        //        foreach (myPoint itemPoint in itemLine.OriPoints)
        //        {
        //            double dNewY = itemPoint.X * itemLine.a + itemLine.b;

        //            myPoint NewPoint = new myPoint();
        //            NewPoint.X = itemPoint.X;
        //            NewPoint.Y = dNewY;
        //            NewPoint.Index = itemPoint.Index;

        //            itemLine.NewPoints.Add(NewPoint);

        //        }
        //    }
        //}

        /// <summary>
        /// 获得已处理的点的个数.
        /// </summary>
        /// <param name="mPoints">所有点的List</param>
        /// <returns>处理过的点的个数.</returns>
        private int GetHandleNum(List<myPoint> mPoints)
        {
            int nRtn = 0;

            foreach (myPoint each in mPoints)
            {
                if (true == each.Handle)
                {
                    nRtn++;
                }
            }

            return nRtn;
        }

        /// <summary>
        /// 输入一个当前点和点List
        /// </summary>
        /// <param name="nCurrentPoint"></param>
        /// <param name="mPoints"></param>
        /// <returns>拟合的直线的结束点Index,作为下一个直线的开始点</returns>
        //private int GetLine(int nCurrentPoint, List<myPoint> mPoints)
        //{
        //    int nReIndex = 0;

        //    // 定义一个临时的点List
        //    List<myPoint> tempPoints = new List<myPoint>();

        //    // 从当前点开始处理，向后寻找点数
        //    int nSum = mPoints.Count;
        //    for (int i = nCurrentPoint; i < nSum; i++)
        //    {
        //        // 向缓冲List增加数据后
        //        myPoint mypoint = mPoints[i];
        //        tempPoints.Add(mypoint);

        //        // Check if it can create a line.
        //        // 判断是否加入到临时点List的点数能否创建一条直线
        //        // 有可能会改变tempPoints的值，
        //        // 如果需要创建，则一定用tempPoints的首尾值来创建
        //        bool bCheck = CheckCreateLine(tempPoints, i);
        //        if (true == bCheck)
        //        {
        //            // 如果可以创建,则退出循环.
        //            // 可以在这里创建
        //            myPoint startPoint = tempPoints[0];
        //            myPoint endPoint = tempPoints[tempPoints.Count - 1];

        //            myLine eachLine = CreateOneLine(startPoint, endPoint);
        //            SetHandle(tempPoints, true);


        //            AddPointsToLine(eachLine, tempPoints);
        //            mLines.Add(eachLine);


        //            nReIndex = eachLine.endpoint.Index;
        //            break;
        //        }
        //    }

        //    // 当直线拟合成功时，返回结束点的Index
        //    // 这个Index会和Point自己保存在一起
        //    return nReIndex;
        //}

        /// <summary>
        /// 为直线设置点的对象值.
        /// </summary>
        /// <param name="eachLine"></param>
        /// <param name="tempPoints"></param>
        private void AddPointsToLine(myLine eachLine, List<myPoint> tempPoints)
        {
            foreach (myPoint each in tempPoints)
            {
                eachLine.OriPoints.Add(each);
            }
        }

        /// <summary>
        /// 不停的加入点List后 判断能不能拟合直线.
        /// </summary>
        /// <param name="tempPoints">点List</param>
        /// <returns>能创建一直线时，返回true
        /// 创建不了时，返回false，继续增加点</returns>
        //private bool CheckCreateLine(List<myPoint> tempPoints, int Index)
        //{
        //    bool bRtn = false;

        //    int nTempSum = tempPoints.Count;
        //    int nSum = mPointsOne.Count;

        //    if ((1 == nTempSum) || (0 == nTempSum))
        //    {
        //        bRtn = false;
        //    }
        //    else if (2 == nTempSum)
        //    {
        //        // 如果加入的点是最后一个点.
        //        // 那么直接拟合直线

        //        if (Index == (nSum - 1))
        //        {
        //            // 只是返回状态，不做创建
        //            bRtn = true;
        //        }
        //    }
        //    else
        //    {
        //        // 首先第一点和最后一点做直线
        //        // 这里有3个点以上

        //        myPoint startPoint = tempPoints[0];
        //        myPoint endPoint = tempPoints[nTempSum - 1];

        //        myLine tempLine = CreateOneLine(startPoint, endPoint);
        //        // 检查在这其中的点,距离这条线是否偏差太大

        //        // 返回true，代表都满足条件，不偏差
        //        // 这里做个判断，如果加入的是最后一个点，则直接生成直线
        //        // 如果不是最后一个点，返回false,继续加入点
        //        bool bCheck = CheckOffset(tempLine, tempPoints);
        //        if (true == bCheck)
        //        {
        //            // 如果没有任意一个点偏离直线，则继续加入点
        //            // 这里做个判断，如果加入的是最后一个点，则直接生成直线                
        //            if (Index == (nSum - 1))
        //            {
        //                // 直接生成直线.
        //                // 用来生成直线的点都需要设置一个处理值.
        //                //myLine addnewLine = CreateOneLine(startPoint, endPoint);
        //                //SetHandle(tempPoints, true);
        //                //mLines.Add(addnewLine);
        //                bRtn = true;
        //            }
        //            else
        //            {
        //                // 如果不是最后一个点，返回false,继续加入点
        //                bRtn = false;
        //            }
                    
        //        }
        //        else
        //        {
        //            // 如果有一个点偏离直线. 则把temp最后一个新加入的点删除.
        //            // 返回true 来创建直线.
        //            tempPoints.RemoveAt(nTempSum - 1);
        //            bRtn = true;
        //        }
        //    }

        //    return bRtn;
        //}

        /// <summary>
        /// 对点List设置处理值.
        /// </summary>
        /// <param name="tempPoints"></param>
        /// <param name="p"></param>
        private void SetHandle(List<myPoint> tempPoints, bool p)
        {
            foreach (myPoint each in tempPoints)
            {
                each.Handle = p;
            }
        }

        /// <summary>
        /// 检查所有位于List point中的所有点
        /// 是否偏离拟定直线
        /// </summary>
        /// <param name="tempLine"></param>
        /// <param name="tempPoints"></param>
        /// <returns></returns>
        //private bool CheckOffset(myLine tempLine, List<myPoint> tempPoints)
        //{
        //    bool bRtn = true;

        //    // 检查所有位于List point中的所有点
        //    // 开始点到结束点之间
        //    int nNum = tempPoints.Count;

        //    // 检查所有位于List point中的所有点 不包括开始点和结束点
        //    for (int i = 1; i < (nNum - 1); i++)
        //    {
        //        myPoint each = tempPoints[i];

        //        // 把这个点的X值输入直线 可以得到直线上的值.
        //        double dNewY = GetYFromLine(tempLine, each.X);

        //        // 然后用这个新的值和 这个点的Y值作对比，如果超过某个间隔时，
        //        // 认为不符合条件

        //        double dOldY = each.Y;
        //        double dOffset = Math.Abs(dNewY - dOldY);

        //        if (dOffset > MAX_OFFSET)
        //        {
        //            bRtn = false;
        //            break;
        //        }
        //    }
        //    return bRtn;
        //}

        /// <summary>
        /// 输入X值，返回Y值
        /// </summary>
        /// <param name="tempLine">直线</param>
        /// <param name="p">X值</param>
        /// <returns>Y值</returns>
        private double GetYFromLine(myLine tempLine, double p)
        {
            double nDRtn = tempLine.a * p + tempLine.b;

            return nDRtn;
        }

        /// <summary>
        /// 通过两点构建一条直线
        /// </summary>
        /// <param name="p1">开始点</param>
        /// <param name="p2">结束点</param>
        /// <returns>一条直线</returns>
        //private myLine CreateOneLine(myPoint p1, myPoint p2)
        //{
        //    myLine reLine = new myLine();

        //    double dA = (p1.Y - p2.Y) / (p1.X - p2.X);
        //    double dB = p1.Y - dA * p1.X;

        //    reLine.a = dA;
        //    reLine.b = dB;
        //    reLine.startpoint = p1;
        //    reLine.endpoint = p2;

        //    return reLine;
        //}

        /// <summary>
        /// 显示数据
        /// </summary>
        //private void ShowLine()
        //{

        //    foreach (myPoint each in mPointsOne)
        //    {
        //        double dY = each.Y;
        //        myChart.Series["1#"].Points.Add(dY);
        //    }

        //    foreach (myPoint each in mPointsTwo)
        //    {
        //        double dY = each.Y;
        //        myChart.Series["2#"].Points.Add(dY);
        //    }

        //    foreach (myPoint each in mPointsThree)
        //    {
        //        double dY = each.Y;
        //        myChart.Series["3#"].Points.Add(dY);
        //    }

        //    //foreach (myPoint each in mPoints_1_2)
        //    //{
        //    //    double dY = each.Y;
        //    //    myChart.Series["12#"].Points.Add(dY);
        //    //}

        //    //foreach (myPoint each in mPoints_1_3)
        //    //{
        //    //    double dY = each.Y;
        //    //    myChart.Series["13#"].Points.Add(dY);
        //    //}

        //    //foreach (myPoint each in mPoints_2_3)
        //    //{
        //    //    double dY = each.Y;
        //    //    myChart.Series["23#"].Points.Add(dY);
        //    //}

        //    //foreach (myPoint each in mPoints_1_2_3)
        //    //{
        //    //    double dY = each.Y;
        //    //    myChart.Series["123#"].Points.Add(dY);
        //    //}

        //    //foreach (myPoint each in mDesignSample)
        //    //{
        //    //    double dY = each.Y;
        //    //    myChart.Series["Sample"].Points.Add(dY);
        //    //}
            
        //}





        /// <summary>
        /// 读取txt数据
        /// </summary>
        /// <param name="strFile">txt文件名字</param>
        /// <returns>一个集合对象</returns>
        private List<myPoint> LoadFile(string strFile)
        {
            List<myPoint> reList = new List<myPoint>();

            int nCount = 0;

            StreamReader sr = new StreamReader(strFile);
            String line;
            while ((line = sr.ReadLine()) != null)
            {
                // 对line进行分析 加入到一个对象里
                //double nRead = Convert.ToDouble(line);

                Decimal deciY = Convert.ToDecimal(line);

                myPoint eachPoint = new myPoint();
                eachPoint.X = 0.0m;
                eachPoint.Y = deciY;
                eachPoint.Index = nCount;

                // 加入List
                reList.Add(eachPoint);


                // 循环变量
                nCount++;
            }

            sr.Close();
            sr.Dispose();

            return reList;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // 累加单位
            Decimal dCount = 1.0m;
            //Decimal dCount = 0.1m;
            //Decimal dCount = 0.01m;  // 10ms


            // 由一个公式生成一个每秒对应一个压力值.

            // 数字处理全部用Decimal格式
            // f = -0.25 * x + 60.
            // mSample1_200

            Decimal deciY = 200.0m;
            Decimal dA = -0.25m;
            Decimal dB = 200.0m;

            //double y = 60.0;

            int nIndex = 0;
            Decimal deciX = 0.0m;

            while (deciY > 0.0m)
            {
                myPoint eachPoint = new myPoint();
                eachPoint.Index = nIndex;
                eachPoint.X = deciX;

                // f = -0.25 * x + 60.
                eachPoint.Y = dA * deciX + dB;
                mSample1_200.Add(eachPoint);

                deciY = eachPoint.Y;
                deciX = deciX + dCount;

                nIndex++;
            }



            SaveFile(mSample1_200, "mSample1_200mpa_10ms.txt");

            Console.WriteLine();


            foreach (myPoint each in mSample1_200)
            {
                Double dY = Convert.ToDouble(each.Y);
                myChart.Series["Sample"].Points.Add(dY);
            }

        }

        private void btnLine2_Click(object sender, EventArgs e)
        {

            // Produce60();


            // Produce140();


            Produce200();

            
        }

        private void Produce200()
        {
            // 分段生成函数.
            // 需要处理保压的逻辑
            Console.WriteLine();

            // 累加单位
            //Decimal dCount = 1.0m;
            Decimal dCount = 0.1m;


            // 第一段.
            // f = -0.2 * x + 60.
            Decimal dA = -0.2m;
            Decimal dB = 200.0m;
            int nIndex = 0;
            Decimal deciY = 200.0m;
            Decimal deciX = 0.0m;

            while (deciY > 140.0m)
            {
                myPoint eachPoint = new myPoint();
                eachPoint.Index = nIndex;
                eachPoint.X = deciX;

                // f = -0.25 * x + 60.
                eachPoint.Y = dA * deciX + dB;

                mSample1_200.Add(eachPoint);

                deciY = eachPoint.Y;
                deciX = deciX + dCount;

                nIndex++;
            }


            Console.WriteLine();


            // 第一段保压10分钟
            // 1s数据的逻辑是600s，增加600个定值点.
            // 100ms 增加6000个点.
            int nCount = 0;
            if (1.0m == dCount)
            {
                nCount = 600;
            }
            else if (0.1m == dCount)
            {
                nCount = 6000;
            }

            for (int i = 0; i < nCount; i++)
            {
                myPoint eachPoint = new myPoint();
                eachPoint.Index = nIndex;
                eachPoint.X = deciX;

                // f = -0.25 * x + 60.
                eachPoint.Y = 140.0m;

                mSample1_200.Add(eachPoint);
            }

            Console.WriteLine();



            // 第二段.
            // f = -0.3 * x + 40. 
            dA = -0.3m;
            dB = 140.0m;
            nIndex = 0;
            deciY = 140.0m;
            deciX = 0.0m;

            while (deciY > 80.0m)
            {
                myPoint eachPoint = new myPoint();
                eachPoint.Index = nIndex;
                eachPoint.X = deciX;

                // f = -0.25 * x + 60.
                eachPoint.Y = dA * deciX + dB;

                mSample1_200.Add(eachPoint);

                deciY = eachPoint.Y;
                deciX = deciX + dCount;

                nIndex++;
            }


            Console.WriteLine();


            // 第二段保压5分钟
            // 1s数据的逻辑是300s，增加300个定值点.
            // 100ms 增加3000个点.
            nCount = 0;
            if (1.0m == dCount)
            {
                nCount = 300;
            }
            else if (0.1m == dCount)
            {
                nCount = 3000;
            }
            for (int i = 0; i < nCount; i++)
            {
                myPoint eachPoint = new myPoint();
                eachPoint.Index = nIndex;
                eachPoint.X = deciX;

                // f = -0.25 * x + 60.
                eachPoint.Y = 80.0m;

                mSample1_200.Add(eachPoint);
            }

            Console.WriteLine();


            // 第三段.
            // f = -0.25 * x + 25. 
            dA = -0.25m;
            dB = 80.0m;
            nIndex = 0;
            deciY = 80.0m;
            deciX = 0.0m;
            while (deciY > 0.0m)
            {
                myPoint eachPoint = new myPoint();
                eachPoint.Index = nIndex;
                eachPoint.X = deciX;

                // f = -0.25 * x + 60.
                eachPoint.Y = dA * deciX + dB;

                mSample1_200.Add(eachPoint);

                deciY = eachPoint.Y;
                deciX = deciX + dCount;

                nIndex++;
            }

            Console.WriteLine();



            SaveFile(mSample1_200, "mSample2_200mpa_100ms.txt");

            Console.WriteLine();


            foreach (myPoint each in mSample1_200)
            {
                Double dY = Convert.ToDouble(each.Y);
                myChart.Series["Sample"].Points.Add(dY);
            }
        }

        private void Produce140()
        {
            // 分段生成函数.
            // 需要处理保压的逻辑
            Console.WriteLine();

            // 累加单位
            // Decimal dCount = 1.0m;
            Decimal dCount = 0.1m;


            // 第一段.
            // f = -0.2 * x + 60.
            Decimal dA = -0.2m;
            Decimal dB = 140.0m;
            int nIndex = 0;
            Decimal deciY = 140.0m;
            Decimal deciX = 0.0m;

            while (deciY > 110.0m)
            {
                myPoint eachPoint = new myPoint();
                eachPoint.Index = nIndex;
                eachPoint.X = deciX;

                // f = -0.25 * x + 60.
                eachPoint.Y = dA * deciX + dB;

                mSample1_200.Add(eachPoint);

                deciY = eachPoint.Y;
                deciX = deciX + dCount;

                nIndex++;
            }


            Console.WriteLine();


            // 第一段保压5分钟
            // 1s数据的逻辑是300s，增加300个定值点.
            // 100ms 增加3000个点.
            int nCount = 0;
            if (1.0m == dCount)
            {
                nCount = 300;
            }
            else if (0.1m == dCount)
            {
                nCount = 3000;
            }

            for (int i = 0; i < nCount; i++)
            {
                myPoint eachPoint = new myPoint();
                eachPoint.Index = nIndex;
                eachPoint.X = deciX;

                // f = -0.25 * x + 60.
                eachPoint.Y = 110.0m;

                mSample1_200.Add(eachPoint);
            }

            Console.WriteLine();



            // 第二段.
            // f = -0.3 * x + 40. 
            dA = -0.3m;
            dB = 110.0m;
            nIndex = 0;
            deciY = 110.0m;
            deciX = 0.0m;

            while (deciY > 50.0m)
            {
                myPoint eachPoint = new myPoint();
                eachPoint.Index = nIndex;
                eachPoint.X = deciX;

                // f = -0.25 * x + 60.
                eachPoint.Y = dA * deciX + dB;

                mSample1_200.Add(eachPoint);

                deciY = eachPoint.Y;
                deciX = deciX + dCount;

                nIndex++;
            }


            Console.WriteLine();


            // 第二段保压5分钟
            // 1s数据的逻辑是300s，增加300个定值点.
            // 100ms 增加3000个点.
            nCount = 0;
            if (1.0m == dCount)
            {
                nCount = 300;
            }
            else if (0.1m == dCount)
            {
                nCount = 3000;
            }
            for (int i = 0; i < nCount; i++)
            {
                myPoint eachPoint = new myPoint();
                eachPoint.Index = nIndex;
                eachPoint.X = deciX;

                // f = -0.25 * x + 60.
                eachPoint.Y = 50.0m;

                mSample1_200.Add(eachPoint);
            }

            Console.WriteLine();


            // 第三段.
            // f = -0.25 * x + 25. 
            dA = -0.25m;
            dB = 50.0m;
            nIndex = 0;
            deciY = 50.0m;
            deciX = 0.0m;
            while (deciY > 0.0m)
            {
                myPoint eachPoint = new myPoint();
                eachPoint.Index = nIndex;
                eachPoint.X = deciX;

                // f = -0.25 * x + 60.
                eachPoint.Y = dA * deciX + dB;

                mSample1_200.Add(eachPoint);

                deciY = eachPoint.Y;
                deciX = deciX + dCount;

                nIndex++;
            }

            Console.WriteLine();



            SaveFile(mSample1_200, "mSample2_140mpa_100ms.txt");

            Console.WriteLine();


            foreach (myPoint each in mSample1_200)
            {
                Double dY = Convert.ToDouble(each.Y);
                myChart.Series["Sample"].Points.Add(dY);
            }
        }

        private void Produce60()
        {
            // 分段生成函数.
            // 需要处理保压的逻辑
            Console.WriteLine();

            // 累加单位
            // Decimal dCount = 1.0m;
            Decimal dCount = 0.1m;


            // 第一段.
            // f = -0.2 * x + 60.
            Decimal dA = -0.2m;
            Decimal dB = 60.0m;
            int nIndex = 0;
            Decimal deciY = 60.0m;
            Decimal deciX = 0.0m;

            while (deciY > 40.0m)
            {
                myPoint eachPoint = new myPoint();
                eachPoint.Index = nIndex;
                eachPoint.X = deciX;

                // f = -0.25 * x + 60.
                eachPoint.Y = dA * deciX + dB;

                mSample1_200.Add(eachPoint);

                deciY = eachPoint.Y;
                deciX = deciX + dCount;

                nIndex++;
            }


            Console.WriteLine();


            // 第一段保压5分钟
            // 1s数据的逻辑是300s，增加300个定值点.
            // 100ms 增加3000个点.
            int nCount = 0;
            if (1.0m == dCount)
            {
                nCount = 300;
            }
            else if (0.1m == dCount)
            {
                nCount = 3000;
            }

            for (int i = 0; i < nCount; i++)
            {
                myPoint eachPoint = new myPoint();
                eachPoint.Index = nIndex;
                eachPoint.X = deciX;

                // f = -0.25 * x + 60.
                eachPoint.Y = 40.0m;

                mSample1_200.Add(eachPoint);
            }

            Console.WriteLine();



            // 第二段.
            // f = -0.3 * x + 40. 
            dA = -0.3m;
            dB = 40.0m;
            nIndex = 0;
            deciY = 40.0m;
            deciX = 0.0m;

            while (deciY > 25.0m)
            {
                myPoint eachPoint = new myPoint();
                eachPoint.Index = nIndex;
                eachPoint.X = deciX;

                // f = -0.25 * x + 60.
                eachPoint.Y = dA * deciX + dB;

                mSample1_200.Add(eachPoint);

                deciY = eachPoint.Y;
                deciX = deciX + dCount;

                nIndex++;
            }


            Console.WriteLine();


            // 第二段保压5分钟
            // 1s数据的逻辑是300s，增加300个定值点.
            // 100ms 增加3000个点.
            nCount = 0;
            if (1.0m == dCount)
            {
                nCount = 300;
            }
            else if (0.1m == dCount)
            {
                nCount = 3000;
            }
            for (int i = 0; i < nCount; i++)
            {
                myPoint eachPoint = new myPoint();
                eachPoint.Index = nIndex;
                eachPoint.X = deciX;

                // f = -0.25 * x + 60.
                eachPoint.Y = 25.0m;

                mSample1_200.Add(eachPoint);
            }

            Console.WriteLine();


            // 第三段.
            // f = -0.25 * x + 25. 
            dA = -0.25m;
            dB = 25.0m;
            nIndex = 0;
            deciY = 25.0m;
            deciX = 0.0m;
            while (deciY > 0.0m)
            {
                myPoint eachPoint = new myPoint();
                eachPoint.Index = nIndex;
                eachPoint.X = deciX;

                // f = -0.25 * x + 60.
                eachPoint.Y = dA * deciX + dB;

                mSample1_200.Add(eachPoint);

                deciY = eachPoint.Y;
                deciX = deciX + dCount;

                nIndex++;
            }

            Console.WriteLine();



            SaveFile(mSample1_200, "mSample2_60mpa_100ms.txt");

            Console.WriteLine();


            foreach (myPoint each in mSample1_200)
            {
                Double dY = Convert.ToDouble(each.Y);
                myChart.Series["Sample"].Points.Add(dY);
            }
        }

        private void btnLine3_Click(object sender, EventArgs e)
        {
            Console.WriteLine();

            // 累加单位
            //Decimal dCount = 1.0m;
            //Decimal dCount = 0.1m;
            Decimal dCount = 0.01m;


            Decimal deciC = -0.00003m;
            Decimal deciD = -0.0005m;
            Decimal deciE = 200.0m;

            int nIndex = 0;
            Decimal deciY = 200.0m;
            Decimal deciX = 0.0m;

            // 抛物线.
            while (deciY > 80.0m)
            {
                myPoint eachPoint = new myPoint();
                eachPoint.Index = nIndex;
                eachPoint.X = deciX;

                // f = -0.25 * x + 60.
                eachPoint.Y = deciC * deciX * deciX + deciD * deciX + deciE;

                if (eachPoint.Y > 80.0m)
                {
                    mSample1_200.Add(eachPoint);
                }

                deciY = eachPoint.Y;
                deciX = deciX + dCount;

                nIndex++;
            }

            Console.WriteLine();

            // 保压1分钟 保持在60
            int nCount = 0;
            if (1.0m == dCount)
            {
                nCount = 60;
            }
            else if (0.1m == dCount)
            {
                nCount = 600;
            }
            else if (0.01m == dCount)
            {
                nCount = 6000;
            }

            for (int i = 0; i < nCount; i++)
            {
                myPoint eachPoint = new myPoint();
                eachPoint.Index = nIndex;
                eachPoint.X = deciX;

                eachPoint.Y = 80.0m;

                mSample1_200.Add(eachPoint);
            }

            Console.WriteLine();

            // 第二段抛物线.


            deciC = -0.00003m;
            deciD = -0.0005m;
            deciE = 80.0m;

            nIndex = 0;
            deciY = 80.0m;
            deciX = 0.0m;

            while (deciY > 0.0m)
            {
                myPoint eachPoint = new myPoint();
                eachPoint.Index = nIndex;
                eachPoint.X = deciX;

                // f = -0.25 * x + 60.
                eachPoint.Y = deciC * deciX * deciX + deciD * deciX + deciE;

                if (eachPoint.Y > 0.0m)
                {
                    mSample1_200.Add(eachPoint);
                }

                deciY = eachPoint.Y;
                deciX = deciX + dCount;

                nIndex++;
            }

            Console.WriteLine();




            SaveFile(mSample1_200, "mSample3_200mpa_10ms.txt");

            Console.WriteLine();


            foreach (myPoint each in mSample1_200)
            {
                Double dY = Convert.ToDouble(each.Y);
                myChart.Series["Sample"].Points.Add(dY);
            }
        }

        private void btnLine4_Click(object sender, EventArgs e)
        {

            // 累加单位
            // Decimal dCount = 1.0m;
            Decimal dCount = 0.1m;

            // 抛物线

            Decimal deciC = -0.00003m;
            Decimal deciD = -0.0001m;
            Decimal deciE = 200.0m;

            int nIndex = 0;
            Decimal deciY = 200.0m;
            Decimal deciX = 0.0m;

            // 抛物线.
            while (deciY > 120.0m)
            {
                myPoint eachPoint = new myPoint();
                eachPoint.Index = nIndex;
                eachPoint.X = deciX;

                // f = -0.25 * x + 60.
                eachPoint.Y = deciC * deciX * deciX + deciD * deciX + deciE;

                if (eachPoint.Y > 120.0m)
                {
                    mSample1_200.Add(eachPoint);
                }

                deciY = eachPoint.Y;
                deciX = deciX + dCount;

                nIndex++;
            }

            Console.WriteLine();

            // 保压1分钟 保持在60
            int nCount = 0;
            if (1.0m == dCount)
            {
                nCount = 60;
            }
            else if (0.1m == dCount)
            {
                nCount = 600;
            }
            for (int i = 0; i < nCount; i++)
            {
                myPoint eachPoint = new myPoint();
                eachPoint.Index = nIndex;
                eachPoint.X = deciX;

                eachPoint.Y = 120.0m;

                mSample1_200.Add(eachPoint);
            }

            Console.WriteLine();


            // 一次函数
            // f = -0.2 * x + 60.
            Decimal dA = -0.2m;
            Decimal dB = 120.0m;
            nIndex = 0;
            deciY = 120.0m;
            deciX = 0.0m;

            while (deciY > 0.0m)
            {
                myPoint eachPoint = new myPoint();
                eachPoint.Index = nIndex;
                eachPoint.X = deciX;

                // f = -0.25 * x + 60.
                eachPoint.Y = dA * deciX + dB;

                mSample1_200.Add(eachPoint);

                deciY = eachPoint.Y;
                deciX = deciX + dCount;

                nIndex++;
            }

            Console.WriteLine();

            SaveFile(mSample1_200, "mSample4_200mpa_100ms.txt");

            Console.WriteLine();


            foreach (myPoint each in mSample1_200)
            {
                Double dY = Convert.ToDouble(each.Y);
                myChart.Series["Sample"].Points.Add(dY);
            }
        }

        private void btnCompare_Click(object sender, EventArgs e)
        {
            // 读取#12
            mPoints_1_2 = LoadFile("#12.txt");
            foreach (myPoint each in mPoints_1_2)
            {
                double dY = Convert.ToDouble(each.Y);
                myChart.Series["#12"].Points.Add(dY);
            }


            // 读取#3
            mPointsThree = LoadFile("#3.txt");

            foreach (myPoint each in mPointsThree)
            {
                double dY = Convert.ToDouble(each.Y);
                myChart.Series["#3"].Points.Add(dY);
            }

            // 找出相同点.
            // 找出差值，写出文件.

            ProduceOffFile("#3.txt", "#3_off.txt");
            ProduceOffFile("#12.txt", "#12_off.txt");
        }

        private void ProduceOffFile(string Origin, string Destination)
        {
            List<myPoint> tempList = LoadFile(Origin);

            List<myPoint> produceList = new List<myPoint>();
            // 从头可以，但是要略去最后一个数.
            int nCount = tempList.Count;

            for (int i = 0; i < (nCount - 1); i++)
            {
                Decimal eachOff = tempList[i].Y - tempList[i + 1].Y;

                myPoint eachNew = new myPoint();
                eachNew.X = 0.0m;
                eachNew.Index = i;
                eachNew.Y = eachOff;

                produceList.Add(eachNew);
            }

            Console.WriteLine();

            SaveFile(produceList, Destination);
        }

        private void btnLoadLine_Click(object sender, EventArgs e)
        {


            OpenFileDialog fileDialog = new OpenFileDialog();
            fileDialog.Multiselect = false;
            fileDialog.Title = "请选择文件";
            fileDialog.Filter = "所有文件(*.*)|*.*";

            string chooseFile = string.Empty;


            if (fileDialog.ShowDialog() == DialogResult.OK)
            {
                chooseFile = fileDialog.FileName;

            }

            // 保护
            if (0 == String.Compare(String.Empty, chooseFile))
            {
                return;
            }


            // 读取文件到工艺曲线 模板里.
            // 发送线程里用来判断.
            ShowLine(chooseFile);
        }

        private void ShowLine(string chooseFile)
        {
            List<myPoint> showPoint = LoadFileToList(chooseFile);

            foreach (myPoint each in showPoint)
            {
                Double dY = Convert.ToDouble(each.Y);
                myChart.Series["Sample"].Points.Add(dY);
            }
        }

        /// <summary>
        /// 读取txt到list里
        /// </summary>
        /// <param name="strFileName"></param>
        /// <returns></returns>
        private List<myPoint> LoadFileToList(string strFileName)
        {
            // 健壮函数.
            if (0 == String.Compare(String.Empty, strFileName))
            {
                return null;
            }

            List<myPoint> reList = new List<myPoint>();

            StreamReader sr = new StreamReader(strFileName);
            String line = String.Empty;
            int nCount = 0;
            while ((line = sr.ReadLine()) != null)
            {
                // 对line进行分析 加入到一个对象里.
                //Double nEach = Convert.ToDouble(line);

                myPoint eachPoint = new myPoint();

                Decimal deciY = Convert.ToDecimal(line);

                //nCount
                eachPoint.Index = nCount;
                eachPoint.X = 0.0m;
                eachPoint.Y = deciY;

                reList.Add(eachPoint);


                //累加.
                nCount++;
            }

            sr.Close();
            sr.Dispose();

            return reList;
        }

    }
}
